#!/usr/bin/env python
#coding=utf-8
#system default setting
import sys
import os
sys.path.append("..")

from structure import wordStruct
from module import TAGModule

import PUBLICMETHOD

import platform


#system default setting
reload(sys)
sys.setdefaultencoding('utf-8')

#configuration file
#########################################################################################################
#store all the config data including the words should be deleted and the words have their weight, and read the data that store the ontology information
class Config:
	isSetDelWordDic = 0
	isSetWeiWordDic = 0
	isReadOntologyDAT = 0
	isSetTagWordSeqNoDic = 0


	@staticmethod
	def init():
		#a dictionary: set up the index between word queue and its first alphabet
		
		BASE_DIR = os.path.dirname(os.getcwd())
		if platform.system() == 'Windows':
			delWordPath = BASE_DIR + "\\code_test\\config\\config_del.ini"
			weiWordPath = BASE_DIR + "\\code_test\\config\\config_wei.ini"
			ontologyPath = BASE_DIR + "\\code_test\\config\\ontologies.dat"
			TagDATPath = BASE_DIR + "\\resource\\TAG\\tags_fixed.dat"
		elif platform.system() == 'Linux':
			delWordPath = BASE_DIR + "/code/config/config_del.ini"
			weiWordPath = BASE_DIR + "/code/config/config_wei.ini"
			ontologyPath = BASE_DIR + "/code/config/ontologies.dat"
			TagDATPath = BASE_DIR + "/resource/TAG/tags_fixed.dat"

		self.__delWordDic = {}
		self.__delWordDicIndexStr = ""
		self.__weiWordDic = {}
		self.__weiWordDicIndexStr = ""
		#a dictionary: set up the index of ontology and it's easy to get its name
		self.__ontologyDic = {}

		self.setDelWordDic(delWordPath)
		self.setWeiWordDic(weiWordPath)
		self.readOntologyDAT(ontologyPath)
		self.setTagWordSeqNoDic(TagDATPath)

	#read the file about some words should be deleted
	@staticmethod
	def setDelWordDic(self, delWordPath):
		print "1111111111111"
		print self.isSetDelWordDic
		print "1111111111111"

		if self.isSetDelWordDic == 0:
			lineContent = PUBLICMETHOD.ReadFile.readConfig(delWordPath)
			for line in lineContent:
				isIndexPos = True
				lineData = line.split()
				wordQueue = wordStruct.WordQueue()
				wordQueue.setFirstAlph(lineData[0])
				wordAscii = ord(lineData[0])
				for data in lineData:
					if isIndexPos == True:
						isIndexPos = False
					else:
						wordQueue.addWord(data)
				self.__delWordDicIndexStr += lineData[0]
				self.__delWordDic[wordAscii] = wordQueue
				del lineData
			del lineContent
			PUBLICMETHOD.ReadFile.closeFile()
			##test code##
			"""
			for item in self.__delWordDic:
				print self.__delWordDic[item].getWordQueueContent()
			"""
			self.isSetDelWordDic = 1

	@staticmethod
	def getDelWordDic(self):
		return self.__delWordDic

	@staticmethod
	def getDelWordDicIndexStr(self):
		return self.__delWordDicIndexStr
		#print self.__delWordDicIndexStr

	@staticmethod
	#read the file about some words that uses to set up the other word's weight
	def setWeiWordDic(self, weiWordPath):
		print "2222222222222"
		print self.isSetWeiWordDic
		print "2222222222222"

		if self.isSetWeiWordDic == 0:
			lineContent = PUBLICMETHOD.ReadFile.readConfig(weiWordPath)
			for line in lineContent:
				isIndexPos = True
				lineData = line.split()
				wordQueue = wordStruct.WordQueue()
				wordQueue.setFirstAlph(lineData[0])
				wordAscii = ord(lineData[0])
				for data in lineData:
					if isIndexPos == True:
						isIndexPos = False
					else:
						#the least five char are the wei about that word, such as (3,4)
						wordContent = data[:-5]
						#the weight about the word before the weiWord, such as 3 in (3,4)
						beforeWeight = data[-4:-3]
						#the weight about the word after the weiWord, such as 4 in (3,4)
						afterWeight = data[-2:-1]
						wordQueue.addWeiWord(beforeWeight, afterWeight, wordContent)			
				self.__weiWordDicIndexStr += lineData[0]
				self.__weiWordDic[wordAscii] = wordQueue
				del lineData
			del lineContent

			PUBLICMETHOD.ReadFile.closeFile()

			self.isSetWeiWordDic = 1
			##test code##

			"""
			for item in self.__weiWordDic:
				print self.__weiWordDic[item].getWordQueueContent()
			"""

	@staticmethod
	def getWeiWordDic(self):
		return self.__weiWordDic

	@staticmethod
	def getWeiWordDicIndexStr(self):
		return self.__weiWordDicIndexStr

	@staticmethod
	def readOntologyDAT(self, ontologyPath):
		print "3333333333333"
		print self.isReadOntologyDAT
		print "3333333333333"

		if self.isReadOntologyDAT == 0:			
			lineContent = PUBLICMETHOD.ReadFile.readDAT(ontologyPath)

			for line in lineContent:
				#delete "\r" and "\n" in the end of line
				line = line[:-2]
				#print line
				ontologyInform = line.split('\t')
				#ontologyInform = line[0].split('\t')
				#print line
				#print ontologyInform
				self.__ontologyDic[ontologyInform[0]] = ontologyInform[2]
			#print self.__ontologyDic[str(11)]
			#print id(lineContent)
			del lineContent
			PUBLICMETHOD.ReadFile.closeFile()
			self.isReadOntologyDAT = 1

	@staticmethod
	def getOntologyDic(self):
		return self.__ontologyDic

	@staticmethod
	def setTagWordSeqNoDic(self, TagDATPath):
		print "4444444444444"
		print self.isSetTagWordSeqNoDic
		print "4444444444444"

		if self.isSetTagWordSeqNoDic == 0:
			self.tagWordSeqNoDic = TAGModule.TagWordSeqNoDic()
			print "reading DAT file..."
			self.tagWordSeqNoDic.readDAT(TagDATPath)
			print "DAT file imports finished!"
			self.isSetTagWordSeqNoDic = 1


	@staticmethod
	def getTagWordSeqNoDic(self):
		return self.tagWordSeqNoDic
#########################################################################################################